package com.wyl.wyl.testapplication.utils.request;

import android.content.Context;

import com.inossem_library.request.http.util.RetrofitUtils;
import com.wyl.wyl.testapplication.constant.RetrofitConstant;
import com.wyl.wyl.testapplication.constant.UrlConstant;
import com.wyl.wyl.testapplication.utils.SP.SPUtils;
import com.wyl.wyl.testapplication.utils.acache.ACacheUtils;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;

/**
 * Created by 34221 on 2019/8/7.
 * <p>
 * Retrofit工具类
 */
public class RequestUtils {

    /**
     * 获取Retrofit实例
     *
     * @return Retrofit实例
     */
    public static Retrofit getRetrofit(Context context) {
        initRetrofit(context);
        return RetrofitUtils.getRetrofit(context, "", "", "", "");
    }

    /**
     * 初始化Retrofit(设置请求头和一些基础参数)
     */
    private static void initRetrofit(Context context) {
        Map<String, String> header = new HashMap<>();
        header.put(RetrofitConstant.CONTENT_TYPE, "application/json;charset=UTF-8");
        header.put(RetrofitConstant.RS, RetrofitConstant.RS_VALUE);
        try {
            RetrofitUtils.set(context, UrlConstant.DEFAULT_HTTP
                            + ACacheUtils.getDomain() + ":"
                            + ACacheUtils.getPort()
                            + UrlConstant.DEFAULT_PATH,
                    header, 20_000L, 20_000L, 20_000L, true, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Retrofit实例
     *
     * @return Retrofit实例
     */
    public static Retrofit getRetrofit(Context context, String domain, String port) {
        initRetrofit(context, domain, port);
        return RetrofitUtils.getRetrofit(context, "", "", "", "");
    }

    /**
     * 初始化Retrofit(设置请求头和一些基础参数)
     */
    private static void initRetrofit(Context context, String domain, String port) {
        Map<String, String> header = new HashMap<>();
        header.put(RetrofitConstant.CONTENT_TYPE, "application/json;charset=UTF-8");
        header.put(RetrofitConstant.RS, RetrofitConstant.RS_VALUE);
        try {
            RetrofitUtils.set(context, UrlConstant.DEFAULT_HTTP
                            + domain + ":"
                            + port
                            + UrlConstant.DEFAULT_PATH,
                    header, 20_000L, 20_000L, 20_000L, true, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
